package com.example.onlinetaxi.ui.activity.MainPage;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.example.onlinetaxi.BaiDuMap.BaiDuMapManager;
import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.callback.MapCallBack.IPickLocation;
import com.example.onlinetaxi.BaiDuMap.MyGeoCoder;
import com.example.onlinetaxi.BaiDuMap.RoutePlan.RoutePlan;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.ui.activity.SearchPage.EndSearchActivity;
import com.example.onlinetaxi.ui.activity.SearchPage.StartSearchActivity;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;

import java.util.HashMap;
import java.util.Map;

// 地图打车界面
public class MapFragment extends Fragment implements View.OnClickListener {

    // 根控件
    private View rootView;
    // 控件
    private MapView mMapView;  // 地图
    private ImageView mMyLocation;  // 返回当前位置按钮
    private TextView mapCityTv;  // 当前城市
    private TextView pickupPointTv;  // 当前场所
    // 获取布局
    private LinearLayout startLayout;
    private LinearLayout endLayout;
    // 跳转
    Intent intent;
    // 当前城市
    private String city;
    // 传入参数
    private Map<String, String> map = new HashMap<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 显示加载框
        MainActivity.closeProgressDialog();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_map, container, false);

        initView();

        return rootView;
    }

    private void initView() {
        // 初始化控件
        mMapView = rootView.findViewById(R.id.bmapView);

        // 城市提示框
        mapCityTv = rootView.findViewById(R.id.map_city_tv);

        // 上车点提示框
        pickupPointTv = rootView.findViewById(R.id.map_pickup_point);

        // 上车下车输入框
        startLayout = rootView.findViewById(R.id.map_start_search);
        endLayout = rootView.findViewById(R.id.map_end_search);

        // 初始化地图管理者
        BaiDuMapManager.baiDuMapManager.setActivity(getActivity());  // 先传递依附主体
        BaiDuMapManager.baiDuMapManager.setiCityLocation(new IPickLocation() {
            @Override
            public void getPickInfo(ReverseGeoCodeResult reverseGeoCodeResult) {
                // 设置城市
                city = reverseGeoCodeResult.getAddressDetail().city;
                // 将用户所在城市保存在缓存/前端数据库
                if (LocalStorage.city == null || !LocalStorage.city.equals(city)) {
                    // 保存到数据库
                    saveCity(city);
                    // 保存本地城市
                    LocalStorage.city = city;
                }

                mapCityTv.setText(reverseGeoCodeResult.getAddressDetail().city);
                // 设置上车地点
                // 报错为空时，需要在手机权限中开启该app的位置权限
                if (reverseGeoCodeResult.getPoiList() != null) {
                    pickupPointTv.setText(reverseGeoCodeResult.getPoiList().get(0).name);
                    // 设置路径查询起点
                    RoutePlan.startMap.put("city", city);
                    RoutePlan.startMap.put("place", reverseGeoCodeResult.getPoiList().get(0).name);
                }
            }
        });
        // 开启定位和反地理编码
        BaiDuMapManager.baiDuMapManager.isLocate(true);
        BaiDuMapManager.baiDuMapManager.isOpenGeoReverse(true);
        BaiDuMapManager.baiDuMapManager.setMapView(mMapView);
        // 初始化“回到当前位置”按钮
        mMyLocation = rootView.findViewById(R.id.my_location);
        // 添加点击事件
        mMyLocation.setOnClickListener(this);
        startLayout.setOnClickListener(this);
        endLayout.setOnClickListener(this);
    }

    // 将用户定位到的城市传入后端
    private void saveCity(String city) {
        map.put("phone", LocalStorage.phone);
        map.put("city", city);

        OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "passenger", "saveCity");
    }

    public void setFirstLocation() {
        // 确保每次加载时都进行定位
        BaiDuMapManager.baiDuMapManager.isFirstLocate = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_location:
                // 回归到当前位置
                BaiDuMapManager.baiDuMapManager.backToLocation();
                break;
            case R.id.map_start_search:
                // 跳转到选择起点界面
                intent = new Intent(getActivity(), StartSearchActivity.class);
                startActivity(intent);
                break;
            case R.id.map_end_search:
                // 跳转到选择终点界面
                intent = new Intent(getActivity(), EndSearchActivity.class);
                startActivity(intent);
                break;

        }
    }

    @Override
    public void onResume() {
        super.onResume();

        if (BaiDuMapManager.baiDuMapManager.mMapView == null) {
            BaiDuMapManager.baiDuMapManager.setMapView(mMapView);
        }

        // 在activity执行onResume时必须调用mMapView.onResume()
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时必须调用mMapView.onPause()
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        BaiDuMapManager.baiDuMapManager.destroy();

        MyGeoCoder.myGeoCoder.destroy();
    }
}